package com.summarecon.database;

import com.summarecon.singleton.UserSession;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseUser{
	private static final String TABLE_USER = "user_data";

    private SQLiteDatabase db;  
    private Context context = null;  
    private DatabaseHelper DBHelper;
    
    public DatabaseUser(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
    
    public void open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
    }

      
    public void close() 
    {
        DBHelper.close();
    }
    
    public boolean Login(String username, String password) throws SQLException 
    {
    	Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_USER + " WHERE user_nik=? AND user_pass=?", new String[]{username,password});
        if (mCursor != null) {           
            if(mCursor.getCount() > 0)
            {
            	if  (mCursor.moveToFirst()) {
                    do {
                    	int id = mCursor.getInt(mCursor.getColumnIndex("user_id"));
                    	UserSession.setId(id);
                    }while (mCursor.moveToNext());
                }
            	return true;
            }
        }
     return false;
    }    
}
